#include "login_dialog.h"
#include "ui_login_dialog.h"

#include "global_variant.h"
QString g_examineExamUnityAccount;
QString g_routeExamUnityAccount;
QString g_examineEquipmentUsername;
QString g_routeEquipmentUsername;

#include <QMessageBox>
#include <QDebug>
#include <QBitmap>
#include <QPainter>

LoginDialog::LoginDialog(QWidget *parent)
    : QDialog(parent, Qt::FramelessWindowHint)
    , ui(new Ui::LoginDialog)
{
    ui->setupUi(this);

    initializeSetStyleSheet();

    initializeSetSlots();
}

LoginDialog::~LoginDialog()
{
    delete ui;
}

void LoginDialog::removeInterfaceAngularity()
{
    //棱角圆滑
    QBitmap bmp(this->size());
    bmp.fill();
    QPainter p(&bmp);
    p.setPen(Qt::NoPen);
    p.setBrush(Qt::black);
    p.setPen(Qt::transparent);//表示RGBA值为(0,0,0,0)的透明色。
    p.setRenderHint(QPainter::Antialiasing); // 反锯齿;
    p.drawRoundedRect(bmp.rect(), 20, 20);
    setMask(bmp);
}

void LoginDialog::initializeSetStyleSheet()
{
    // //设置标题
    // this->setWindowTitle("选择人员登录");
    // //隐藏问号选项
    // setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint);


    //关闭按钮
    ui->closePushButton->setStyleSheet("QPushButton{border-image: url(:/Image/closeButton.png)}");

    //设置表格样式
    ui->userInformationTableView->setStyleSheet("QTableView{background-color: rgba(64, 143, 212, 30%);color:black;}");

    userInformationSqlQueryModel = new QSqlQueryModel(this);
    userInformationSelection = new QItemSelectionModel(userInformationSqlQueryModel);

    //只允许选中行
    ui->userInformationTableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    //单选模式
    ui->userInformationTableView->setSelectionMode(QAbstractItemView::SingleSelection);
    //隔行变色
    ui->userInformationTableView->setAlternatingRowColors(true);

    ui->userInformationTableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    ui->userInformationTableView->verticalHeader()->hide();

    removeInterfaceAngularity();
}

void LoginDialog::initializeSetSlots()
{
    //关闭按钮
    connect(ui->closePushButton, &QAbstractButton::clicked,
            this, &LoginDialog::close);

    //根据姓名的输入补全用户信息
    connect(ui->usernameLineEdit, &QLineEdit::textChanged,
            this, [&](QString content){

                QSqlDatabase db = QSqlDatabase::database(m_databaseConnectedName);
                QString sql = QString("SELECT student_id,name,sex FROM student_information WHERE name LIKE \'%%1%\'").arg(content);
                userInformationSqlQueryModel->setQuery(sql, db);

                userInformationSqlQueryModel->setHeaderData(0, Qt::Horizontal, tr("学号"));
                userInformationSqlQueryModel->setHeaderData(1, Qt::Horizontal, tr("姓名"));
                userInformationSqlQueryModel->setHeaderData(2, Qt::Horizontal, tr("性别"));

                ui->userInformationTableView->setModel(userInformationSqlQueryModel);
                ui->userInformationTableView->setSelectionModel(userInformationSelection);

            });

    connect(ui->userInformationTableView, &QTableView::doubleClicked,
            this, [&](){

                QModelIndex curIndex = userInformationSelection->currentIndex();
                QString account = userInformationSqlQueryModel->data(userInformationSqlQueryModel->index(curIndex.row(), 0)).toString();
                QString username = userInformationSqlQueryModel->data(userInformationSqlQueryModel->index(curIndex.row(), 1)).toString();

                if(m_equipmentType == ClientHashEnum::ExamineExamUnityEquipment){
                    g_examineExamUnityAccount = account;
                    g_examineEquipmentUsername = username;
                }else if(m_equipmentType == ClientHashEnum::RouteExamUnityEquipment){
                    g_routeExamUnityAccount = account;
                    g_routeEquipmentUsername = username;
                }

                //防止两台设备账号重复登录
                if(g_examineExamUnityAccount == g_routeExamUnityAccount){
                    auto button = QMessageBox::warning(this, tr("信息提示"), QString("设备登录账号有重复，请退出另一台设备的账号，再登录，谢谢！"));
                    if(button == 0){

                    }
                }else{
                    this->close();

                    emit show2MainInterface(username, m_equipmentType, m_examType, m_examPatternType, m_faultType);
                }
            });
}

void LoginDialog::initializeLoginDialog(int equipmentType, int examType, int examPatternType)
{
    m_equipmentType = equipmentType;
    m_examType = examType;
    m_examPatternType = examPatternType;

    ui->usernameLineEdit->clear();
    userInformationSqlQueryModel->clear();
    ui->userInformationTableView->setModel(userInformationSqlQueryModel);

    this->exec();
}

void LoginDialog::initializeLoginDialog(int equipmentType, int examType, int examPatternType, QHash<QString, QList<int> > faultType)
{
    m_equipmentType = equipmentType;
    m_examType = examType;
    m_examPatternType = examPatternType;
    m_faultType = faultType;

    ui->usernameLineEdit->clear();
    userInformationSqlQueryModel->clear();
    ui->userInformationTableView->setModel(userInformationSqlQueryModel);

    this->exec();
}
